package com.ruoyi.house_sale.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.house_sale.domain.*;
import com.ruoyi.house_sale.service.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 房产资料Controller
 * 
 * @author ruoyi
 * @date 2022-03-29
 */
@RestController
@RequestMapping("/house_sale/houseinfo")
public class HouseinfoController extends BaseController
{
    @Autowired
    private IHouseinfoService houseinfoService;
    @Resource
    ITermtypeinfoService termtypeinfoService;
    @Resource
    IHousemodelService housemodelService;
    @Resource
    ITerminfoService terminfoService;
    @Resource
    ISalestateService salestateService;
    /**
     * 查询房产资料列表
     */
    @PreAuthorize("@ss.hasPermi('house_sale:houseinfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(Houseinfo houseinfo)
    {
        startPage();
        List<Houseinfo> list = houseinfoService.selectHouseinfoList(houseinfo);
        return getDataTable(list);
    }

    @GetMapping("/others")
    public AjaxResult others(){

        List<Termtypeinfo> list = termtypeinfoService.findAllTermType();
        List<Housemodel> list1 = housemodelService.findAllHouseModel();
        List<Terminfo> list2 = terminfoService.findAllTerm();
        List<Salestate> list3 = salestateService.findAllSaleState();
        AjaxResult ajax = AjaxResult.success();
        ajax.put("termtypeinfoList", list);
        ajax.put("housemodelList", list1);
        ajax.put("terminfoList", list2);
        ajax.put("salestateList", list3);
        return ajax;
    }
    //判断是否已结清
    @GetMapping("/updIsBalance/{hourseid}")
    public AjaxResult updIsBalance(@PathVariable("hourseid") Long hourseid){

        return toAjax(houseinfoService.updisbalance(hourseid));
    }

    /**
     * 导出房产资料列表
     */
    @PreAuthorize("@ss.hasPermi('house_sale:houseinfo:export')")
    @Log(title = "房产资料", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Houseinfo houseinfo)
    {
        List<Houseinfo> list = houseinfoService.selectHouseinfoList(houseinfo);
        ExcelUtil<Houseinfo> util = new ExcelUtil<Houseinfo>(Houseinfo.class);
        util.exportExcel(response, list, "房产资料数据");
    }

    /**
     * 获取房产资料详细信息
     */
    @PreAuthorize("@ss.hasPermi('house_sale:houseinfo:query')")
    @GetMapping(value = "/{hourseid}")
    public AjaxResult getInfo(@PathVariable("hourseid") Long hourseid)
    {
        List<Termtypeinfo> list = termtypeinfoService.findAllTermType();
        List<Housemodel> list1 = housemodelService.findAllHouseModel();
        List<Terminfo> list2 = terminfoService.findAllTerm();
        Houseinfo hourseinfo= houseinfoService.selectHouseinfoByHourseid(hourseid);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("termtypeinfoList", list);
        ajax.put("housemodelList", list1);
        ajax.put("terminfoList", list2);
        ajax.put("hourseinfo", hourseinfo);
        return ajax;
    }

    /**
     * 新增房产资料
     */
    @PreAuthorize("@ss.hasPermi('house_sale:houseinfo:add')")
    @Log(title = "房产资料", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Houseinfo houseinfo)
    {
        return toAjax(houseinfoService.insertHouseinfo(houseinfo));
    }

    /**
     * 修改房产资料
     */
    @PreAuthorize("@ss.hasPermi('house_sale:houseinfo:edit')")
    @Log(title = "房产资料", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Houseinfo houseinfo)
    {
        return toAjax(houseinfoService.updateHouseinfo(houseinfo));
    }

    /**
     * 删除房产资料
     */
    @PreAuthorize("@ss.hasPermi('house_sale:houseinfo:remove')")
    @Log(title = "房产资料", businessType = BusinessType.DELETE)
	@DeleteMapping("/{hourseids}")
    public AjaxResult remove(@PathVariable Long[] hourseids)
    {
        return toAjax(houseinfoService.deleteHouseinfoByHourseids(hourseids));
    }

    /**
     * 下载房产资料模板
     */
    @PreAuthorize("@ss.hasPermi('house_sale:schemeflow:export')")
    @Log(title = "下载房产资料模板", businessType = BusinessType.EXPORT)
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Houseinfo> util = new ExcelUtil<Houseinfo>(Houseinfo.class);
        util.importTemplateExcel(response, "房产资料");
    }

    /**
     * 导入房产资料数据
     */
    @Log(title = "房产资料", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:user:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Houseinfo> util = new ExcelUtil<Houseinfo>(Houseinfo.class);
        List<Houseinfo> houseinfoList = util.importExcel(file.getInputStream());
        String message = houseinfoService.importHouseinfo(houseinfoList, updateSupport);
        return AjaxResult.success(message);
    }
}
